<template>
  <div class="upload-container">
    <el-upload
      :data="dataObj"
      name="file_single"
      :multiple="false"
      :show-file-list="false"
      :on-success="handleImageSuccess"
      class="image-uploader"
      :headers="headers"
      :accept="acceptType"
      :action="action"
    >
      <el-button size="small" type="primary">上传附件</el-button>
    </el-upload>
  </div>
</template>

<script>
import { notifyError } from '@/utils/message'
import { apipath } from '@/utils/config'
import { getToken } from '@/utils/auth'
export default {
  name: 'EditorSlideUploadFile',
  props: {
    color: {
      type: String,
      default: '#1890ff'
    }
  },
  data() {
    return {
      acceptType: '', // 允许文件类型
      filepath: null,
      dialogVisible: false,
      listObj: {},
      fileList: [],
      accept: '',
      imageUrl: '',
      tempUrl: '',
      backinfo: [],
      action: '/admin/attachment/upload',
      headers: { token: '' }, // 请求头
      dataObj: { type: 'file', storage: 'public' } // 请求其它参数
    }
  },
  created() {
    this.action = apipath + this.action
    this.headers.token = getToken()
  },
  methods: {
    handleImageSuccess(response, file) {
      console.log(response)
      if (response.code !== 1) {
        notifyError(response.msg || '上传失败，请重试')
        return false
      }
      this.$emit('successCBK', [response.data])
    },
    checkAllSuccess() {
      return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
    },
    handleSubmit() {
      const arr = Object.keys(this.listObj).map(v => this.listObj[v])
      if (!this.checkAllSuccess()) {
        this.$message('请等待所有图片上传成功')
        return
      }
      this.$emit('successCBK', arr)
      this.listObj = {}
      this.fileList = []
      this.dialogVisible = false
    },
    handleSuccess(response, file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          this.listObj[objKeyArr[i]].url = response.files.file
          this.listObj[objKeyArr[i]].hasSuccess = true
          return
        }
      }
      this.$emit('successCBK', this.listObj)
    },
    handleRemove(file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]]
          return
        }
      }
    },
    beforeUpload(file) {
      const _self = this
      const _URL = window.URL || window.webkitURL
      const fileName = file.uid
      this.listObj[fileName] = {}
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.src = _URL.createObjectURL(file)
        img.onload = function() {
          _self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: this.width, height: this.height }
        }
        resolve(true)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.editor-slide-upload {
  margin-bottom: 20px;
  ::v-deep .el-upload--picture-card {
    width: 100%;
  }
}
</style>
